package com.ygsoft.tianlu_mall.controller;

import com.ygsoft.tianlu_mall.pojo.Msg;
import com.ygsoft.tianlu_mall.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class CartController {

    @Autowired
    CartService cartService;

    @Autowired
    CollectController collectController;

    //添加商品到购物车
    @RequestMapping(value = "/addGoodsToCart", method = RequestMethod.POST)
    @ResponseBody
    public Msg addGoodsToCart(@RequestParam int user_id,@RequestParam int goods_id){


        List checkGoodsInCart = cartService.checkGoodsInCart(user_id,goods_id);
        List checkGoodsNotMine = cartService.checkGoodsNotMine(user_id,goods_id);
        //禁止重复添加商品到购物车 结果为空允许放行
        if (checkGoodsInCart.size()==0){
            //禁止将自己店铺出售的商品到购物车 结果为空允许放行
            if (checkGoodsNotMine.size()==0){
                cartService.addGoodsToCart(user_id,goods_id);
                return Msg.succes();
            }
        }
        String message = "商品添加到购物车失败，请更换商品！";
        return Msg.fail().add("message",message);
    }

    //从购物车删除商品
    @RequestMapping(value = "/deleteGoodsFromCart", method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteGoodsFromCart(@RequestParam int user_id,@RequestParam int goods_id){
        List checkGoodsInCart = cartService.checkGoodsInCart(user_id,goods_id);
        if (checkGoodsInCart.size()!=0){
            cartService.deleteGoodsFromCart(user_id,goods_id);
            return Msg.succes();
        }
        String message = "删除失败，购物车并没有这个商品！";
        return Msg.fail().add("message",message);
    }

    //获得购物车信息
    @RequestMapping(value = "/getGoodsInfoFromCart", method = RequestMethod.GET)
    @ResponseBody
    public Msg getGoodsInfoFromCart(@RequestParam int user_id){
        try {
            HashMap<String, Object> goodsList = cartService.getGoodsInfoFromCart(user_id);
            Map<String, Object> result = new HashMap<>();
            result.put("goodsList",goodsList);
            return Msg.succes().add("result",result);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //批量管理功能
    //删除
    @RequestMapping(value = "/deleteMoreGoodsFromCart", method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteMoreGoodsFromCart(@RequestParam int user_id,@RequestParam List<Integer> goods_idList){
        try {
            for (int goods_id:goods_idList) {
                deleteGoodsFromCart(user_id,goods_id);
            }
            return Msg.succes();
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //失效
    @RequestMapping(value = "/clearFailedGoods", method = RequestMethod.DELETE)
    @ResponseBody
    public Msg clearFailedGoods(@RequestParam int user_id,@RequestParam List<Integer> goods_idList){
        try {
            for (int goods_id:goods_idList) {
                deleteGoodsFromCart(user_id,goods_id);
                cartService.clearFailedGoods(user_id,goods_id);
            }
            return Msg.succes();
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //收藏
    @RequestMapping(value = "/addAllGoodsToCollect", method = RequestMethod.POST)
    @ResponseBody
    public Msg addAllGoodsToCollect(@RequestParam int user_id,@RequestParam List<Integer> goods_idList){
        try {
            for (int goods_id:goods_idList) {
                deleteGoodsFromCart(user_id,goods_id);
                collectController.collectGoods(user_id,goods_id);
            }
            return Msg.succes();
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //购物车数量
    @RequestMapping(value = "/getCartCount", method = RequestMethod.GET)
    @ResponseBody
    public Msg getCartCount(@RequestParam int user_id){
        try {
            int count = cartService.getCartCount(user_id);
            Map<String, Object> result = new HashMap<>();
            result.put("count",count);
            return Msg.succes().add("result",result);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

}
